package com.json;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class JsonHelper {
	
	private static Gson gson = null;
	private static Logger log = Logger.getLogger(JsonHelper.class);
	
	  public JsonHelper(){
		  
	  
	  }
	  public void initJSON(){
		  try{
				if( null == gson){
				  gson = new Gson();
				} 
		  } catch (Exception e){
			  e.printStackTrace(); 
		  }
	  }
	  //listת��Ϊjson��
	  public String toJSON(List li){
		  try{
			  initJSON();
			  Type typeOfSrc = new TypeToken<ArrayList<HashMap<String,String>>>(){}.getType();
			  return gson.toJson(li,typeOfSrc);
		  } catch(Exception e){
			  e.printStackTrace();
			  log.error(e.getMessage());
		  }
		  return null;  
	  }
	  //mapת��Ϊjson��
	  public String toJSON(Map mp){
		  try{
			  initJSON();
			  return gson.toJson(mp);
		  } catch(Exception e){
			  e.printStackTrace();
			  log.error(e.getMessage());
		  }
		  return null;  
	  }
	  
	  /**  
	   * 将Map准换为JSON字符串  
	   * @param map  
	   * @return JSON字符串  
	   */  
	  public static  String getJsonStringFromMap(Map<?, ?> map) {   
	      JSONObject object = JSONObject.fromObject(map);   
	      return object.toString();   
	  }  
	  
	  /**  
	   * 将Java对象转换为JSON格式的字符串  
	   *   
	   * @param javaObj  
	   *            POJO,例如日志的model  
	   * @return JSON格式的String字符串  
	   */  
	  public static String getJsonStringFromJavaPOJO(Object javaObj) {   
	      return JSONObject.fromObject(javaObj).toString(1);   
	  }  
	  
	  /**  
	   * 读取XML文件准换为JSON字符串  
	   * @param xmlFile  XML文件  
	   * @return JSON字符串  
	   */  
	  public static String getXMLFiletoJSONString(String xmlFile) {   
		  InputStream is = JsonHelper.class.getResourceAsStream(xmlFile);   
	      String xml;   
	      JSON json = null;   
	      try {   
	          xml = IOUtils.toString(is);   
	          XMLSerializer xmlSerializer = new XMLSerializer();   
	          json = xmlSerializer.read(xml);   
	      } catch (IOException e) {   
	          e.printStackTrace();   
	      }   
	      return json.toString();   
	  }  
	  
	  //���ַ�json��ת��Ϊlist
	  public List fromJSON(String str){
		  try{
			  Gson gson = new Gson();
			  Type typeOfSrc = new TypeToken<ArrayList<HashMap<String,String>>>(){}.getType();
			  //String xx= "[{\"yy\":\"yy\",\"xxxx\":\"xxx\"},{\"zz\":\"syts\",\"yy\":\"sdffs\",\"xxxx\":\"dfds\"}]";
			  List oo = gson.fromJson(str, typeOfSrc);
			  return oo;
		  } catch(Exception e){
			  e.printStackTrace();
			  log.error(e.getMessage());
		  }
		  return null;  
	  }
	 //json��ת��Ϊlist
	  public List fromJSON(InputStream in){
		  try{
			  
				BufferedReader in1 = new BufferedReader(new InputStreamReader(in,"utf-8"));
				String temp;
				StringBuffer str = new StringBuffer();
				while((temp = in1.readLine()) != null) {
					str.append(temp);
				}     
				in.close();
				//List lis = fromJSON(new String(str.toString().getBytes("utf-8"),"GBK"));
				List lis = fromJSON(str.toString());
			  return lis;
		  } catch(Exception e){
			  e.printStackTrace();
			  log.error(e.getMessage());
		  }
		  return null;  
	  }
	  
	  //调试
	  public static void main (String[] args)
	  {
		  String xmlFile = "E:\\Workspaces\\knowledgePoints\\src\\com\\dom4j\\createAndparseXml\\holen.xml";
		  getXMLFiletoJSONString(xmlFile);
	  }
}
